package com.s63.controller;

import com.s63.entity.User;
import com.s63.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    protected UserService userService;

    @GetMapping
    public String getAll(Model model) {
        model.addAttribute("userList", userService.list());
        return "user/index";
    }

    @GetMapping("/add")
    public String add() {
        return "user/add";
    }

    @GetMapping("/update")
    public String update(Integer id, Model model) {
        model.addAttribute("user", userService.getById(id));
        return "user/update";
    }
    @PostMapping("/update")
    public String update(User user) {
        userService.updateById(user);
        return "redirect:/users";
    }

    @GetMapping("/del")
    public String del(Integer id) {
        userService.removeById(id);
        return "redirect:/users";
    }

    @PostMapping
    public String add(User user) {
        userService.save(user);
        return "redirect:/users";
    }
}
